#include "administrator.h"
#include "ui_administrator.h"
#include "QStandardItem"
#include "QTableWidgetItem"
#include "addlayout.h"
#include "cinemasystem.h"

Administrator::Administrator(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Administrator)
{
    ui->setupUi(this);
    QStandardItemModel *model = new QStandardItemModel();


    CinemaSystem cinemasystem("2023-10-17", "Sample Movie", 100.0);   //括号里的参数没用
    cinemasystem.halls= cinemasystem.readAuditoriumsFromFile("D:\\QT_projects\\Login\\2.txt");//从这里读取场次信息
    unsigned int total_hall_number = cinemasystem.halls.size();

    model->setRowCount(int(total_hall_number));
    model->setColumnCount(4);
    model->setRowCount(total_hall_number);
    //QString film_data = QString::fromStdString(cinemasystem.halls[0].movie["2000-01-01"].first);

    for (int row = 0; row < total_hall_number; ++row) {
        for (int col = 0; col < 3; ++col) {
            QModelIndex index = model->index(row, col);
            if(col == 0){
                //QString film_date = QString::fromStdString(cinemasystem.halls[row].movie);//电影名
                //model->setData(index,film_date);
                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
                QString film_time = QString::fromStdString(__filmtime);//电影时间
                model->setData(index,film_time);
            }else if(col == 1){
                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
                QString film_name = QString::fromStdString(cinemasystem.halls[row].movie[__filmtime].first);//电影名
                model->setData(index,film_name);
            }else if(col == 2){
                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
                std::string _prise = std::to_string(cinemasystem.halls[row].movie[__filmtime].second);
                QString film_prise = QString::fromStdString(_prise);//价格
                model->setData(index,film_prise);
            }else if(col == 3){

            }

        }
    }
    ui->tableView->horizontalHeader()->resizeSection(0, 225);
    //ui->tableView->columnWidth(0);
    ui->tableView->setModel(model);
}

Administrator::~Administrator()
{
    delete ui;
}


void Administrator::on_pushButton_clicked()
{

    AddLayout *add_layout = new AddLayout(this);
    add_layout->show();
    //this->hide();
}


void Administrator::on_pushButton_2_clicked()
{
    // 读取文件内容
    std::ifstream inputFile("D:\\QT_projects\\Login\\2.txt");
    std::vector<std::string> lines;
    std::string line;
    while (std::getline(inputFile, line)) {
        lines.push_back(line);
    }
    inputFile.close();

    // 删除最后四行
    size_t numLines = lines.size();
    if (numLines <= 4) {
        lines.clear();
    } else {
        lines.erase(lines.end() - 4, lines.end());
    }

    // 将修改后的内容写回文件
    std::ofstream outputFile("D:\\QT_projects\\Login\\2.txt");
    for (const std::string& line : lines) {
        outputFile << line << std::endl;
    }
    outputFile.close();
    Administrator *admist = new Administrator(this);
    admist->show();

}
